import {
	mapState,
	mapMutations
} from "vuex"
import {
	queryAddresById,
	deleteAddresById,
	addAddressBook
} from "./api.js"
import address from "../index.js";
export default {
	data() {
		return {
			edit: false,
			addressId: null, 
			address: {
				consignee: '',
				phone:'',
				detail: ''
			}
		}
	},
	onShow() {
		this.onShowCall();
	},
	onLoad: function(option) {
		this.addressId = option.addressId;
		if(this.addressId !== null && this.addressId !== undefined){
			this.edit = true;
			this.onLoadCall();
		}
	},
	computed:{
		
	},
	methods: {
		callAddAddressBook(){
	        // 前置校验
			addAddressBook(this.address).then((res)=>{
				if(res.code === 200){
					uni.showToast({
						title: '添加成功!',
						icon: "success",
						duration: 1000,
					});
					this.goBack();
				}else{
					uni.showToast({
						title: res.msg,
						icon: "fail",
						duration: 1000,
					});
				}
			}).catch((error) =>{})
		},
		// 删除按钮
		deleteAddress(){
			const addressId = this.addressId;
			uni.showModal({
			  title: '操作确认',
			  content: '确认要执行此操作吗？',
			  confirmColor: '#FF0000', // 确定按钮为红色
			  cancelColor: '#00FF00',  // 取消按钮为绿色
			  success: function (res) {
			    if (res.confirm) {
					const params = { id: addressId }
					console.log(params);
					deleteAddresById(params).then((res) =>{
						if(res.code === 200){
							uni.showToast({
								title: '删除成功',
								icon: "success",
								duration: 1000,
							});
						}else{
							uni.showToast({
								title: res.msg,
								icon: "fail",
								duration: 1000,
							});
						}
					}).catch((error) =>{})
					this.goBack();
			    } else if (res.cancel) {
			    }
			  }
			});
		},
		addressFromWx(){
			uni.showToast({
				title: '为了信息安全，暂不允许从微信导入地址！',
				icon: "none",
				duration: 1000,
			});
		},
		onLoadCall(){
			const params = { id: this.addressId }
			queryAddresById(params).then((res) =>{
				this.address = res.data;
				// console.log('address', this.address);
			}).catch((res)=>{});
		},
		goBack() {
			uni.navigateBack({ delta: 1 })
		},
		// 修改默认的地址
		changeDefaultAddress(item){
			
		},
		onShowCall(){
			
		},
		//   vuex储存信息
		...mapMutations([
			"setShopInfo", //设置店铺信息
			"setShopPhone", //设置电话
			"setShopStatus", //设置店铺状态
			"initdishListMut", //设置购物车订单
			"setStoreInfo",
			"setBaseUserInfo", //设置用户基本信息
			"setLodding",
			"setToken", //设置token
			"setDeliveryFee", //设置配送费
		]),
		// 从vuex信息
		...mapState([
			"shopInfo", //店铺信息
			"shopPhone", //电话
			"orderListData",
			"baseUserInfo", //用户信息
			"lodding",
			"token", //token
			"deliveryFee", //配送费
		]),
		
		
	}

}